<!-- 业务规则 -->
<template>
  <div class="full-container">
    <page-main>
      <div class="title">
        <span class="line" /><span>添加业务</span>
      </div>
      <div v-if="active !== 3" class="step-form">
        <div class="step">
          <el-steps :active="active" align-center>
            <el-step title="规则信息" />
            <el-step title="规则明细" />
            <el-step title="选择模板" />
            <el-step title="完成" />
          </el-steps>
        </div>
        <div class="form-wrapper">
          <div v-if="active === 0">
            <StepOne ref="stepOne" />
          </div>
          <div v-else-if="active === 1">
            <StepTwo ref="stepTwo" />
          </div>
          <div v-else-if="active === 2">
            <StepThree ref="stepThree" />
          </div>
          <div v-else>
            <StepFour ref="stepFour" />
          </div>
        </div>
        <div v-if="active !== 3" class="btn-wrapper">
          <el-button @click="cancelForm(stepOne)">
            取消
          </el-button>
          <el-button type="primary" @click="nextStep">下一步</el-button>
        </div>
      </div>
      <div v-if="active !== 3" class="des">
        <span class="text">说明：</span>
        <p>业务编码需根据各自业务命名规则自行输入，支持36位以内的包括文字、字母、数字、特殊符号等格式，且命名不可重复；</p>
        <p>业务名称可根据用户使用习惯输入，用于业务规则检索使用，至多支持输入16个文字；</p>
        <p>短训业务规则明细由1校区、2班级类型、3学科、授4课模式、5付费类型、6学员状态，六个个元素组成，且全部元素皆为必选项；</p>
        <p>一个学籍仅支持匹配一种业务规则；</p>
        <p>模板选择不可为空，且仅支持单选；</p>
        <p>规则一旦创建，默认为启用状态，如需禁用，请返回“通用业务管理”页面进行“禁用”操作；</p>
      </div>
      <div v-if="active === 3">
        <StepFour />
      </div>
    </page-main>
  </div>
</template>

<script name="addBusiness">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import StepOne from './stepOne.vue'
import StepTwo from './stepTwo.vue'
import StepThree from './stepThree.vue'
import StepFour from './stepFour.vue'

export default {
  components: { StepOne, StepTwo, StepThree, StepFour },
  setup() {
    const router = useRouter()
    const active = ref(0)
    const stepOne = ref()
    const stepTwo = ref()
    const stepThree = ref()
    // 取消
    const cancelForm = () => {
      // resetForm(formEl.ruleFormRef)
      router.push({ name: 'businessRule' })
    }
    // 下一步
    const nextStep = () => {
      if (active.value === 0) {
        validateFn(stepOne)
      } 
      if (active.value === 1) {
        validateFn(stepTwo)
      } 
      if (active.value === 2) {
        validateFn(stepThree)
      } 
    }
    const validateFn = formEl => {
      formEl.value.ruleFormRef.validate((valid, fields) => {
        if (valid) {
          active.value++
        } else {
          console.log('error submit!', fields)
        }
      })
    }
    // 重置
    const resetForm = formEl => {
      if (!formEl) return
      formEl.resetFields()
    }
    // 提交
    const submitForm = async formEl => {
      if (!formEl) return
      await formEl.validate((valid, fields) => {
        if (valid) {
          console.log('submit!')
        } else {
          console.log('error submit!', fields)
        }
      })
    }
    onMounted(() => {
      let val = router.currentRoute.value.params.active
      if (val) {
        active.value = Number(val)
      }
    })
    return {
      stepOne,
      stepTwo,
      stepThree,
      active,
      cancelForm,
      nextStep,
      submitForm
    }
  }
}

</script>

<style scoped lang="scss">
.page-main {
  position: relative;
  top: 0;
  left: 0;
  padding: 6px 0 0 30px !important;
  font-size: 14px;
}
// 标题
.title {
  font-weight: 500;
  padding: 14px 0;
  border: 1px solid transparent;
  border-bottom-color: #e6e6e6;
  .line {
    display: inline-block;
    width: 4px;
    height: 12px;
    background: #247ffc;
    border-radius: 2px;
    margin-right: 8px;
    vertical-align: middle;
  }
}

// 步骤条、表单
.step-form {
  position: relative;
  top: 0;
  left: 0;
  width: 800px;
  height: calc(100% - 280px);
  padding: 30px 0;
  border: 1px solid transparent;
  border-bottom-color: #e6e6e6;
  // 步骤条
  .step {
    position: absolute;
    top: 30px;
    left: 0;
    right: 0;
    padding: 21px 0 14px;
    border-radius: 6px;
    background: #f5f7fa;
    :deep(.el-step__icon) {
      width: 46px;
      height: 46px;
      font-size: 20px;
      border: none;
      background-color: #e9f2fe;
      color: #333;
    }
    :deep(.el-step__line) {
      top: 23px;
      height: 0;
      border-top: 1px dashed #dbdbdb;
    }
    :deep(.el-step__title) {
      margin-top: 4px;
      font-size: 14px;
      &.is-process {
        color: #247ffc;
      }
    }
    :deep(.is-process) {
      .el-step__icon {
        color: #247ffc;
      }
    }
    :deep(.is-finish) {
      .el-step__icon {
        width: 48px;
        height: 48px;
        background-color: #fff;
        background-image: url("@/assets/images/step-complete.png");
        color: #fff;
        border: none;
      }
    }
    :deep(.el-step__line-inner) {
      border: none;
    }
  }
  // 表单
  .form-wrapper {
    position: absolute;
    top: 148px;
    left: 0;
    right: 0;
    height: calc(100% - 230px);
    overflow: scroll;
    margin-top: 30px;
  }
  // 按钮
  .btn-wrapper {
    position: absolute;
    bottom: 30px;
    left: 350px;
    height: 32px;
  }
}

// 说明
.des {
  position: absolute;
  left: 50px;
  bottom: 30px;
  .text {
    display: inline-block;
    margin-bottom: 7px;
    font-size: 16px;
    font-weight: 500;
  }
  p {
    line-height: 24px;
    font-size: 12px;
    font-weight: 400;
    color: #666;
  }
}
</style>

